package m1;

import com.rabbitmq.client.*;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();
        //创建队列  helloworld
        /*

         */
        c.queueDeclare("hellowoeld", false, false, false, null);

        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String s = new String(a);
                System.out.println("收到："+s);



            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            public void handle(String s) throws IOException {

            }
        };


        //开始接收消息，收到的消息会传递到一个回调对象进行处理
        /*
        2.参数：autoAck
        true :自动确认      相当于一个消息回执，当消费端处理完信息后返回回执后才从队列中删除这个消息
        false: 手动确认
         */

        c.basicConsume("helloworld", true,deliverCallback,cancelCallback);
    }
}
